package com.woniu40.separate_music.web;

import com.woniu40.separate_music.entity.User;
import com.woniu40.separate_music.result.ResponseResult;
import com.woniu40.separate_music.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/user")
@Api
@SessionAttributes({"userInfo"})
public class UserController {
    @Autowired
    UserService userService;
    @PostMapping("/login")
    @ApiOperation(value = "用户登录")
    public ResponseResult<User> login (String username, String upass, Map map){
        User user = userService.findUserByName(username);
        if (user!=null){
            if (user.getUpass().equals(upass)){
                ResponseResult<User> result=new ResponseResult<>();
                result.setCode(200);
                result.setMessage("登录成功");
                result.setData(user);
                map.put("userInfo",user);
                return result;
            }else {
                ResponseResult<User> result=new ResponseResult<>();
                result.setCode(100);
                result.setMessage("密码错误");
                result.setData(user);
                return result;
            }
        }else{
            ResponseResult<User> result=new ResponseResult<>();
            result.setCode(101);
            result.setMessage("用户不存在");

            return result;
        }
    }
}
